package cn.lili.modules.permission.integration.impl;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.service.UserService;
import cn.lili.modules.permission.entity.dos.Role;
import cn.lili.modules.permission.entity.dos.RoleMenu;
import cn.lili.modules.permission.integration.PermissionIntegrationService;
import cn.lili.modules.permission.service.DepartmentService;
import cn.lili.modules.permission.service.MenuService;
import cn.lili.modules.permission.service.RoleMenuService;
import cn.lili.modules.permission.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限相关业务整合中间件
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/4/12 10:09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PermissionIntegrationServiceImpl implements PermissionIntegrationService {

    /**
     * 用户
     */
    private final UserService userService;
    /**
     * 部门
     */
    private final DepartmentService departmentService;
    /**
     * 角色
     */
    private final RoleService roleService;
    /**
     * 菜单角色
     */
    private final RoleMenuService roleMenuService;
    /**
     * 菜单角色
     */
    private final MenuService menuService;

    @Override
    public void userDepartment(List<String> departmentId) {

        List<User> users = userService.getUserByDepartmentIds(departmentId);
        if (users != null && users.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer();
            users.forEach(user -> {
                stringBuffer.append(user.getUsername()).append(",");
            });
            throw new ServiceException(ResultCode.DEPARTMENT_BOUND_BY_USER, stringBuffer.substring(0, stringBuffer.length() - 2));
        }
    }

    @Override
    public void roleUser(List<String> roleId) {

    }

    @Override
    public void roleDepartment(List<String> roleIds) {

    }

    @Override
    public void menuRole(List<String> menuId) {
        List<RoleMenu> roleMenus = roleMenuService.getRoleMenuByMenuIds(menuId);

        if (roleMenus != null && roleMenus.size() > 0) {
            List<String> roleIds = roleMenus.stream().map(RoleMenu::getRoleId).collect(Collectors.toList());
            List<Role> roles = roleService.listByIds(roleIds);

            StringBuffer stringBuffer = new StringBuffer();
            roles.forEach(role -> {
                stringBuffer.append(role.getName()).append(",");
            });
            throw new ServiceException(ResultCode.MENU_BOUND_BY_ROLE, stringBuffer.substring(0, stringBuffer.length() - 2));
        }
    }


    @Override
    public void deleteRoleMenuByRoles(List<String> roleIds) {

    }

    @Override
    public void deleteRoles(List<String> roleIds) {
        //绑定判定
        this.roleUser(roleIds);
        this.roleDepartment(roleIds);

        roleService.deleteRoles(roleIds);
    }

    @Override
    public void deleteDepartments(List<String> departmentIds) {

        //校验部门是否被引用
        this.userDepartment(departmentIds);

        departmentService.deleteDepartments(departmentIds);
    }

    @Override
    public void deleteMenus(List<String> menuIds) {

        //如果已有角色绑定菜单，则不能直接删除
        this.menuRole(menuIds);
        menuService.deleteMenus(menuIds);
    }
}
